{{ define "content" }}
<div class="row">
    <div class="col-12">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2>虚拟环境管理</h2>
            <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#createVenvModal">
                <i class="fas fa-plus"></i> 创建虚拟环境
            </button>
        </div>

        <!-- 虚拟环境列表 -->
        <div class="row">
            {{ range .venvs }}
            <div class="col-md-6 col-lg-4">
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="card-title mb-0">{{ .Name }}</h5>
                    </div>
                    <div class="card-body">
                        <p class="card-text">
                            <i class="fas fa-code"></i> {{ .PythonVersion }}<br>
                            <i class="fas fa-box"></i> {{ len .Packages }} 个已安装的包
                        </p>
                        <div class="mt-3">
                            <button class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#installPackageModal" data-venv="{{ .Name }}">
                                <i class="fas fa-download"></i> 安装包
                            </button>
                            <button class="btn btn-sm btn-info" data-bs-toggle="collapse" data-bs-target="#packages-{{ .Name }}">
                                <i class="fas fa-list"></i> 查看包列表
                            </button>
                            <button class="btn btn-sm btn-danger delete-venv" data-venv="{{ .Name }}">
                                <i class="fas fa-trash"></i> 删除
                            </button>
                        </div>
                        <div class="collapse mt-3" id="packages-{{ .Name }}">
                            <div class="card card-body">
                                <h6>已安装的包：</h6>
                                <ul class="list-unstyled">
                                    {{ range .Packages }}
                                    <li>
                                        {{ .Name }} ({{ .Version }})
                                        <button class="btn btn-sm btn-danger float-end uninstall-package" 
                                                data-venv="{{ $.Name }}" 
                                                data-package="{{ .Name }}">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </li>
                                    {{ end }}
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div class="card-footer">
                        <small class="text-muted">创建于 {{ .CreatedAt.Format "2006-01-02 15:04:05" }}</small>
                    </div>
                </div>
            </div>
            {{ end }}
        </div>
    </div>
</div>

<!-- 创建虚拟环境模态框 -->
<div class="modal fade" id="createVenvModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">创建新的虚拟环境</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <form id="createVenvForm" action="/venv" method="post">
                    <div class="mb-3">
                        <label for="venv_name" class="form-label">环境名称</label>
                        <input type="text" class="form-control" id="venv_name" name="venv_name" required>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="submit" form="createVenvForm" class="btn btn-primary">创建</button>
            </div>
        </div>
    </div>
</div>

<!-- 安装包模态框 -->
<div class="modal fade" id="installPackageModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">安装新的包</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <form id="installPackageForm">
                    <input type="hidden" id="install_venv_name" name="venv_name">
                    <div class="mb-3">
                        <label for="package_name" class="form-label">包名称</label>
                        <input type="text" class="form-control" id="package_name" name="package" required>
                        <div class="form-text">可以指定版本，例如：requests==2.28.1</div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id="installPackageBtn">安装</button>
            </div>
        </div>
    </div>
</div>
{{ end }}

{{ define "scripts" }}
<script>
// 删除虚拟环境
$('.delete-venv').click(function() {
    if (!confirm('确定要删除这个虚拟环境吗？这将删除所有已安装的包。')) {
        return;
    }
    var name = $(this).data('venv');
    $.ajax({
        url: '/venv/' + name,
        type: 'DELETE',
        success: function() {
            location.reload();
        }
    });
});

// 卸载包
$('.uninstall-package').click(function() {
    if (!confirm('确定要卸载这个包吗？')) {
        return;
    }
    var venv = $(this).data('venv');
    var package = $(this).data('package');
    $.ajax({
        url: '/venv/' + venv + '/packages/' + package,
        type: 'DELETE',
        success: function() {
            location.reload();
        }
    });
});

// 安装包模态框
$('#installPackageModal').on('show.bs.modal', function(event) {
    var button = $(event.relatedTarget);
    var venv = button.data('venv');
    $('#install_venv_name').val(venv);
});

// 安装包
$('#installPackageBtn').click(function() {
    var venv = $('#install_venv_name').val();
    var package = $('#package_name').val();
    $.post('/venv/' + venv + '/packages', {package: package})
        .done(function() {
            location.reload();
        })
        .fail(function(xhr) {
            alert('安装失败: ' + xhr.responseJSON.error);
        });
});
</script>
{{ end }} 